#include "mySort.h"
#include <iostream>
#include <sstream>
#include <string>

MySort::MySort() : num(0), origin(nullptr) {}

MySort::MySort(int num, int *origin)
{
    this->num = num;
    this->origin = new int[num];
    for (int i = 0; i < num; i++)
    {
        this->origin[i] = origin[i];
    }
}

MySort::~MySort()
{
    delete[] origin;
}

void MySort::changeSwap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

string MySort::printNum()
{
    stringstream ss;
    for (int i = 0; i < num; i++)
    {
        ss << origin[i] << " ";
    }
    ss << "\n";
    return ss.str();
}

string MySort::paritySort()
{
    bool swap = true;
    string result = "";
    while (swap)
    {
        swap = false;
        for (int i = 1; i < num - 1; i += 2)
        {
            if (origin[i] > origin[i + 1])
            {
                changeSwap(origin[i], origin[i + 1]);
                swap = true;
                result += printNum();
            }
        }
        for (int i = 0; i < num - 1; i += 2)
        {
            if (origin[i] > origin[i + 1])
            {
                changeSwap(origin[i], origin[i + 1]);
                swap = true;
                result += printNum();
            }
        }
    }
    return result;
}

string MySort::bubbleSort()
{
    bool swap = true;
    string result = "";
    for (int i = 0; i < num; i++)
    {
        swap = false;
        for (int j = 0; j < num - 1 - i; j++)
        {
            if (origin[j] > origin[j + 1])
            {
                changeSwap(origin[j], origin[j + 1]);
                swap = true;
                result += printNum();
            }
        }
        for (int j = num - 2 - i; j >= i; --j)
        {
            if (origin[j] > origin[j + 1])
            {
                changeSwap(origin[j], origin[j + 1]);
                swap = true;
                result += printNum();
            }
        }
    }
    return result;
}